package com.collect.senegal.zhiScene.zhiAuditer.zhiFollow.zhiReview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.collect.senegal.R;
import com.collect.senegal.zhiBase.BaseFragment;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAuditerReviewCallogEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAuditerReviewContactEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAuditerReviewEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAuditerReviewUserEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISAuditerReviewMarkedOrder;
import com.collect.senegal.zhiHelper.ZHISysShareHelper;
import com.collect.senegal.zhiScene.zhiAuditer.zhiRoot.ZHIRootAuditerFragmentInterface;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogAvatar;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogItemClickListener;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogWordsInput;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIAuditerReviewFragment extends BaseFragment<ZHIAuditReviewPresenter> implements ZHIAuditReviewContract.View {

    private static final String REVIEW_RESULT_PASS = "WHOLE_PASS";
    private static final String REVIEW_RESULT_REJECT = "WHOLE_REJECT";
    private static final String REVIEW_RESULT_LATER = "WHOLE_WAIT";

    @BindView(R.id.view_no_data)
    LinearLayout view_no_data;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    //顶部个人信息
    @BindView(R.id.image_user_avatar)
    ImageView image_user_avatar;
    @BindView(R.id.text_user_name_value)
    TextView text_user_name_value;
    @BindView(R.id.text_birth_date_value)
    TextView text_birth_date_value;
    @BindView(R.id.text_gender_value)
    TextView text_gender_value;
    @BindView(R.id.text_mobile_value)
    TextView text_mobile_value;
    @BindView(R.id.image_mobile_copy)
    ImageView image_mobile_copy;
    @BindView(R.id.image_mobile_call)
    ImageView image_mobile_call;

    //产品信息
    @BindView(R.id.text_product_value)
    TextView text_product_value;
    @BindView(R.id.text_loan_time_value)
    TextView text_loan_time_value;
    @BindView(R.id.text_later_num_value)
    TextView text_later_num_value;
    @BindView(R.id.text_product_link_value)
    TextView text_product_link_value;
    @BindView(R.id.image_product_link_copy)
    ImageView image_product_link_copy;

    //公司信息
    @BindView(R.id.text_highest_education_value)
    TextView text_highest_education_value;
    @BindView(R.id.text_monthly_income_value)
    TextView text_monthly_income_value;
    @BindView(R.id.text_employment_type_value)
    TextView text_employment_type_value;
    @BindView(R.id.text_company_phone_value)
    TextView text_company_phone_value;

    @BindView(R.id.rc_contact_list)
    RecyclerView rc_contact_list;

    @BindView(R.id.rc_calllog_list)
    RecyclerView rc_calllog_list;

    @BindView(R.id.text_result_pass)
    TextView text_result_pass;
    @BindView(R.id.text_result_reject)
    TextView text_result_reject;
    @BindView(R.id.text_result_later)
    TextView text_result_later;
    private ZHIRootAuditerFragmentInterface mZHIRootAuditerFragmentInterface;

    //order id
    private String mOrderId = "";
    //人脸图片链接
    private String mAvator = "";

    private final ArrayList<ZHIRAuditerReviewContactEntity> mContactList = new ArrayList<>();
    private final ZHIAduiterContactListAdjuster mZHIAduiterContactListAdjuster = new ZHIAduiterContactListAdjuster();

    private final ArrayList<ZHIRAuditerReviewCallogEntity> mCalllogList = new ArrayList<>();
    private final ZHIAduiterCallogListAdjuster mZHIAduiterCallogListAdjuster = new ZHIAduiterCallogListAdjuster();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_auditer_review;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mZHIRootAuditerFragmentInterface = (ZHIRootAuditerFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetAuditerReviewList(ZHIRAuditerReviewEntity zhirAuditerReviewEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            //判断是否有数据
            if (zhirAuditerReviewEntity.getRestOrderNum().equalsIgnoreCase("0")) {
                view_no_data.setVisibility(View.VISIBLE);
            } else {
                view_no_data.setVisibility(View.GONE);
            }

            //个人信息
            ZHIRAuditerReviewUserEntity userInfo = zhirAuditerReviewEntity.getUserInfo();
            text_user_name_value.setText(userInfo.getFullName());
            text_birth_date_value.setText(userInfo.getBirthDate());
            text_gender_value.setText(userInfo.getGender());
            text_mobile_value.setText(userInfo.getMobile());
            mAvator = userInfo.getFaceImg();
            mOrderId = zhirAuditerReviewEntity.getOrderId();

            //产品信息
            text_product_value.setText(userInfo.getProduct());
            text_loan_time_value.setText(userInfo.getApplyCreateTime());
            text_later_num_value.setText(zhirAuditerReviewEntity.getLaterNum());
            text_product_link_value.setText(userInfo.getProductUrl());

            //公司信息
            text_highest_education_value.setText(userInfo.getHighestEducation());
            text_employment_type_value.setText(userInfo.getEmploymentType());
            text_company_phone_value.setText(userInfo.getMobile());
            text_monthly_income_value.setText(userInfo.getMonthlyIncome());

            //联系人
            mContactList.clear();
            mContactList.addAll(zhirAuditerReviewEntity.getUserContact());
            mZHIAduiterContactListAdjuster.setNewData(mContactList);

            //通话记录
            mCalllogList.clear();
            mCalllogList.addAll(zhirAuditerReviewEntity.getPhonebooks());
            mZHIAduiterCallogListAdjuster.setNewData(mCalllogList);

            //加载图片
            if (!TextUtils.isEmpty(mAvator) && (mAvator.startsWith("http"))) {
                Glide.with(requireActivity()).load(mAvator).into(image_user_avatar);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMarkAuditerReviewOrder() {
        try {
            srl_swipe_layout.setRefreshing(false);

            //初始化数据
            initData(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "NotifyDataSetChanged"})
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            //联系人列表
            mZHIAduiterContactListAdjuster.setOnItemClickListener((caseEntity, isDailAction) -> {
                if (isDailAction) {
                    //拨打电话
                    String mobile = caseEntity.getMobile();
                    ZHISysShareHelper.callPhone(getActivity(), mobile, (mobile.length() <= 9));

                } else {
                    //复制
                    String mobile = caseEntity.getMobile();
                    ZHISysShareHelper.copyToClipboard(requireActivity(), mobile);
                }
            });
            rc_contact_list.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            rc_contact_list.setAdapter(mZHIAduiterContactListAdjuster);

            //通话记录列表
            mZHIAduiterCallogListAdjuster.setOnItemClickListener((caseEntity, isDailAction) -> {
                if (isDailAction) {
                    //拨打电话
                    String mobile = caseEntity.getMobile();
                    ZHISysShareHelper.callPhone(getActivity(), mobile, (mobile.length() <= 9));

                } else {
                    //复制
                    String mobile = caseEntity.getMobile();
                    ZHISysShareHelper.copyToClipboard(requireActivity(), mobile);
                }
            });
            rc_calllog_list.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            rc_calllog_list.setAdapter(mZHIAduiterCallogListAdjuster);

            //个人信息
            image_user_avatar.setOnClickListener(view -> {
                //打开头像
                try {
                    if (!TextUtils.isEmpty(mAvator) && mAvator.startsWith("http")) {
                        ZHIDialogAvatar.showDialog(requireActivity(), mAvator);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            image_mobile_copy.setOnClickListener(view -> {
                //复制
                String mobile = text_mobile_value.getText().toString().trim();
                ZHISysShareHelper.copyToClipboard(requireActivity(), mobile);
            });
            image_mobile_call.setOnClickListener(view -> {
                //拨打电话
                String mobile = text_mobile_value.getText().toString().trim();
                ZHISysShareHelper.callPhone(getActivity(), mobile, (mobile.length() <= 9));
            });

            //产品链接
            image_product_link_copy.setOnClickListener(view -> {
                //复制
                String mobile = text_product_link_value.getText().toString().trim();
                ZHISysShareHelper.copyToClipboard(requireActivity(), mobile);
            });

            //审核结果
            text_result_pass.setOnClickListener(view -> {
                //通过
                showRemarkDialog(REVIEW_RESULT_PASS);
            });
            text_result_reject.setOnClickListener(view -> {
                //拒绝
                showRemarkDialog(REVIEW_RESULT_REJECT);
            });
            text_result_later.setOnClickListener(view -> {
                //待会再决定
                showRemarkDialog(REVIEW_RESULT_LATER);
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            mPresenter.getAuditerReviewList(requireActivity(), showLoading);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }

    private void showRemarkDialog(String resultType) {
        ZHIDialogWordsInput.showDialog(requireActivity(), "", getString(R.string.pls_enter), getString(R.string.btn_cancel), getString(R.string.confirm), true, new ZHIDialogItemClickListener() {
            @Override
            public void onConfirmClick(String content) {
                try {
                    if (mPresenter != null) {
                        //保存记录
                        ZHISAuditerReviewMarkedOrder zhisAuditerReviewMarkedOrder = new ZHISAuditerReviewMarkedOrder(mOrderId, resultType, content);
                        mPresenter.markAuditerReviewOrder(requireActivity(), zhisAuditerReviewMarkedOrder, true);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelClick() {

            }
        });
    }

    public void refreshData() {
        initData(true);
    }

}